<%@ page import="login.User" %>
<%--
  Login Page
  This page includes login form. User selects her certificate and enters PIN,
  and applet generates signature, then signature is sent to server via form.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>

<html>
<head>
    <title>Login</title>
</head>
<body style="background-color: #c0c0c0">

<script type="text/javascript">

    /**
     * Called when login is clicked and processed before login form is sent.
     * Calls signFromHTML function in the applet and sends the parameters session id as document
     * to be signed, certificate serial number in hex format and smart card pin respectively
     */
    function secureLogin() {
        var toBeSigned = document.secureLoginForm.sessionid.value;
        var pin = document.secureLoginForm.pin.value;
        //var certOps = document.getElementById("certOptions");
        //var certHex = certOps.options[certOps.selectedIndex].value;

        var certHex = "nocert";
        var certRadios = document.getElementsByName("certRadios");

        for(var i = 0; i < certRadios.length; i++) {
            var cert = certRadios.item(i);
            if(cert.checked) {
                certHex = cert.value;
            }
        }

        if(certHex == "nocert")
            alert("You need to select your certificate.");
        else
            parent.document.webloginapplet.signFromHTML(toBeSigned,certHex,pin);
    }

</script>

    <form name="secureLoginForm" action="loginValidate" method="post">
        <input name="sessionid" type="hidden" value="<% out.write(session.getId()); %>">
        <input id="signature64" name="signature64" type="hidden" value="notinityet"/>
        <div id="certOptions">
        </div>
        PIN:<input name="pin" type="password" value="12345"/>
        <input type="submit" onclick="secureLogin()" value="secure login">
    </form>

<%
    User user = (User) session.getAttribute("user");

    if(user != null && user.getMessage() != "") {

        out.write(user.getMessage());
    }
%>


</body>
</html>